import {addAccount, checkUsername, checkPhone, loginAccount, getAccount, signOutAccount} from "@/api/api";
import {deleteItemfromCollect, checkVerification, getNode, bendNode, getAccountByPhone} from "../../../src/api/api";


export default {
  data() {
    return {
      usernameExist: false,
      emailExist: false,
      usernameNotExist: false,
      phoneNotExist: false,
      messageBox: false,
      username: null,
      password: null,
      phone: null,
      time: null,
      btnText: "Send",
      cacheCode: null,
      loginByUsername: true,
      loginByPhone: false,
      email: null,
      message: "",
      canGet: false,
      timer: null,
      closebtn: false,

    }
  },
  methods: {
    onRegister() {

      if (this.username == null) {
        this.messageBox = true;
        this.message = "Please enter the username";
      } else if (this.password == null) {
        this.messageBox = true;
        this.message = "Please enter the password";
      } else if (this.email == null) {
        this.messageBox = true;
        this.message = "Please enter the Email";
      } else {
        this.checkUsername();
        if (this.usernameExist == false) {


          let param = new FormData;
          param.append("username", this.username);
          param.append("password", this.password);
          param.append("email", this.email);
          addAccount(param).then((res) => {

            if (res.data.status == 0) {
              this.messageBox = true;
              this.message = "Register Success!";

            } else if (res.data.status == 2) {
              alert("注册失败");

            }
          }).catch((res) => {
            alert("注册错误：" + res);
          });
        } else {
          this.messageBox = true;
          this.message = "This account has been registered";
        }
      }
    },
    login() {
      if (this.loginByUsername === true) {


        if (this.username == null) {
          this.messageBox = true;
          this.message = "Please enter the username";
        } else if (this.password == null) {
          this.messageBox = true;
          this.message = "Please enter the password";
        } else {
          this.checkUsername();
          if (this.usernameNotExist === true) {
            alert("用户名不存在")
          } else {
            let param = new FormData;
            param.append("username", this.username);
            param.append("password", this.password);

            loginAccount(param).then((res) => {
              if (res.data.status === 0) {
                console.log(res.data.data.username);

                sessionStorage.setItem("nowUsername", res.data.data.username);
                this.messageBox = true;
                this.closebtn = true;
                this.message = "Login Success！";

              } else {
                alert("密码不正确")
              }

            }).catch((res) => {
              alert("错误：" + res);
            });

          }
        }
      } else {
        if (this.phone == null) {
          this.messageBox = true;
          this.message = "Please enter the PhoneNumber";
        } else if (this.cacheCode == null) {
          this.messageBox = true;
          this.message = "Please enter the cacheCode";
        } else {
          this.checkPhone();
          if (this.phoneNotExist === false) {
            checkVerification(this.cacheCode).then((res) => {

              console.log(res.data)
              console.log(res.data.status)

              if (res.data.status === 1) {
                alert(res.data.msg);
              } else if (res.data.status === 2) {
                alert(res.data.msg);
              } else {

                console.log("this.phone：" + this.phone)

                getAccountByPhone(this.phone).then((res) => {

                  console.log("getAccountByPhone结果: ")
                  console.log(res.data.status)

                  if (res.data.status === 0) {
                    sessionStorage.setItem("nowUsername", res.data.data.username);
                  }

                  this.messageBox = true;
                  this.closebtn = true;
                  this.message = "Login Success！";

                }).catch((res) => {
                  alert("错误34：" + res);
                });

              }

            }).catch((res) => {
              alert("错误33：" + res);
            });
          }
        }
      }


    },
    checkUsername() {
      if (this.username == null) {
        this.usernameExist = false;
        this.usernameNotExist = false;
      } else {
        checkUsername(this.username).then((res) => {
          if (res.data.status == 0) {
            this.usernameExist = true;
            this.usernameNotExist = false;
          } else if (res.data.status == 1) {
            this.usernameNotExist = true;
            this.usernameExist = false;
          }
        }).catch((res) => {
          alert("错误33：" + res);
        });
      }
    },
    checkPhone() {
      if (this.phone == null) {
        this.phoneNotExist = false;
      } else {

        checkPhone(this.phone).then((res) => {
          if (res.data.status === 1) {
            this.phoneNotExist = true;
          } else {
            this.phoneNotExist = false;
          }
        }).catch((res) => {
          alert("错误checkPhone：" + res);
        });
      }
    },

    logByPhone() {
      this.loginByUsername = false;
      this.loginByPhone = true;

    },
    logByUsername() {
      this.loginByUsername = true;
      this.loginByPhone = false;
    },
    timer1() {   //验证码60s内不能继续点击的函数
      if (this.time > 0) {
        this.time--;
        this.btnText = this.time + "s后重新获取"
        this.timer = setTimeout(this.timer1, 1000)
      } else {
        this.time = 0;
        this.btnText = "Send";
        this.canGet = false;
        clearTimeout(this.timer);
      }
    },
    sendVerificate() {
      this.canGet = true;  //禁用按钮，防止多次触发
      this.time = 60;  //60秒后能继续按按钮
      this.timer1();
      console.log(typeof this.phone);
      console.log(this.phone);
      console.log(this.phoneNotExist);
      if (this.phoneNotExist === false) {

        getNode(this.phone).then(res => {

          console.log("getNode的返回结果：");
          console.log(res.data);

          if (res.data.status === 0) {
            alert("验证码发送成功");

          } else if (res.data.status === 1) {
            alert("验证码发送失败");
          }
        }).catch((res) => {
          alert("发送验证码：" + res);
        });
      }
    },
  }
}
